<template>
  <el-dialog title="提示" :visible.sync="show" width="600px">
    <div>
      <el-form ref="ruleForm" :rules="rules" label-position="top" label-width="80px" :model="ruleForm">
        <el-form-item label="课程类型" prop="category_id" required>
          <el-select v-model="ruleForm.category_id" @change="changeCategory">
            <el-option v-for="item in categoryList" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="课程" required prop="course_id">
          <el-select v-model="ruleForm.course_id" placeholder="请选择课程" @change="changeCourse">
            <el-option v-for="item in courseList" :key="item.id" :value="item.id" :label="item.course_name" />
          </el-select>
        </el-form-item>
        <el-form-item label="学生" required>
          <el-select v-model="ruleForm.student_id" filterable placeholder="请选择学生">
            <el-option v-for="item in studentList" :key="item.id" :value="item.id" :label="item.student_name" />
          </el-select>
        </el-form-item>
        <el-form-item label="平时成绩">
          <el-input v-model="ruleForm.score1" type="number" />
        </el-form-item>
        <el-form-item label="期末成绩">
          <el-input v-model="ruleForm.score2" type="number" />
        </el-form-item>
        <el-form-item label="其他类型">
          <el-radio-group v-model="ruleForm.type" class="radio" @change="changeType">
            <el-radio-button label="1">参军</el-radio-button>
            <el-radio-button label="2">缓考</el-radio-button>
            <el-radio-button label="3">退学</el-radio-button>
            <el-radio-button label="4">旷考</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="ruleForm.desc" type="textarea" />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="onAdd">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { mapGetters } from 'vuex'
import { http } from '@/api'

export default {
  name: 'PageAdd',
  data() {
    return {
      show: false,
      ruleForm: {

      },
      rules: {},
      currentItem: {},
      studentList: [],
      courseList: []
    }
  },
  created() {
  },
  computed: {
    ...mapGetters(['categoryList', 'userInfo'])
  },
  methods: {
    open(item) {
      this.ruleForm = { ...item || {
        course_id: '',
        category_id: '',
        teacher_id: '',
        student_id: '',
        score1: '',
        score2: '',
        desc: ''
      }}
      this.show = true
      if (item.id) {
        if (item.category_id) {
          http.course_start.list({
            category_id: item.category_id
          }).then(res => {
            this.courseList = res.map(el => {
              return {
                ...el.course,
                course_start_id: el.id
              }
            })
            http.course_student.list({
              course_start_id: this.courseList.find(el => el.course_id === item.course_id)?.course_start_id
            }).then(res => {
              if (!res.length) {
                this.studentList = []
                this.ruleForm.student_id = ''
                return
              }
              this.getStudentList(res.map(el => el.student_id).join(','))
            })
          })
        }
      }
    },
    onAdd() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$emit('save', {
            ...this.ruleForm,
            teacher_id: this.userInfo.id
          })
        }
      })
    },
    changeType(val) {
      if (val == 4) {
        this.ruleForm.score2 = 0
      } else {
        this.ruleForm.score2 = 60
      }
    },
    changeCategory(val) {
      this.ruleForm.course_id = ''
      http.course_start.list({
        category_id: val
      }).then(res => {
        this.courseList = res.map(el => {
          return {
            ...el.course,
            id: el.category_id,
            course_start_id: el.id
          }
        })
      })
      this.changeCourse('')
    },
    changeCourse(val) {
      this.ruleForm.student_id = ''
      if (val) {
        http.course_student.list({
          course_start_id: this.courseList.find(el => el.course_id === val)?.course_start_id
        }).then(res => {
          if (!res.length) {
            this.studentList = []
            this.ruleForm.student_id = ''
            return
          }
          this.getStudentList(res.map(el => el.student_id).join(','))
        })
      }
    },
    getStudentList(ids) {
      http.student.list({
        ids
      }).then(res => {
        console.log(res)
        this.studentList = res
      })
    }
  }
}
</script>

<style scoped>

</style>
